﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;

namespace Forms
{
    public partial class MarketSet : UIForm
    {
        User user;
        Market market;
        MarketService marketService = new MarketService();
        public MarketSet(User user, Market market)
        {
            SetStyle(
                ControlStyles.UserPaint//使用自定义的绘制方式
                | ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.DoubleBuffer// 双缓冲
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//禁止擦除背景.则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.EnableNotifyMessage// 
                                                   // Enable the OnNotifyMessage event so we get a chance to filter out 
                                                   // Windows messages before they get to the form's WndProc
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            InitializeComponent();
            this.user = user;
            this.market = market;
            if (market != null)
            {
                nameTextBox.Text = market.MarketName;
                addressTextBox.Text = market.MarketAddress;
                timeTextBox.Text = market.MarketOperationTime;
            }
            Color userColor = MainForm.MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }

        // 判断是否有输入为空的现象
        public bool isNull()
        {
            if (nameTextBox.Text == "" || nameTextBox.Text == null)
            {
                UIMessageBox.Show("请输入超市名称");
                return false;
            }
            if (addressTextBox.Text == "" || addressTextBox.Text == null)
            {
                UIMessageBox.Show("请输入超市地址");
                return false;
            }
            if (timeTextBox.Text == "" || timeTextBox.Text == null)
            {
                UIMessageBox.Show("请输入超市运营时间");
                return false;
            }
            return true;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (market == null)
                {
                    if (!isNull()) return;
                    market = new Market();
                    market.MarketID = Guid.NewGuid().ToString();
                    market.MarketName = nameTextBox.Text;
                    market.MarketOperationTime = timeTextBox.Text;
                    market.MarketAddress = addressTextBox.Text;
                    market.UserID = user.UserID;
                    marketService.AddMarket(market);
                    UIMessageBox.ShowSuccess("增加成功！");
                    this.Close();
                }
                else
                {
                    market.MarketName = nameTextBox.Text;
                    market.MarketOperationTime = timeTextBox.Text;
                    market.MarketAddress = addressTextBox.Text;
                    marketService.ModifyMarket(market);
                    UIMessageBox.ShowSuccess("修改成功！");
                    this.Close();
                }
            }
            catch //(DbUpdateException ex)
            {
                UIMessageBox.ShowError("增加失败，请稍后再试");
            }
        }
    }
}
